package com.example.charlotte.snakegame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends BaseActivity {
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private MyDB myDB;
    private SharedPreferences.Editor editor;
    private SharedPreferences saved;
    private GameMusic music;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        saved = PreferenceManager.getDefaultSharedPreferences(this);
        music=new GameMusic(this);

        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);
        editText3 = findViewById(R.id.editText3);
    }

    @Override
    protected void onStart() {
        super.onStart();
        myDB = new MyDB(this, "User.db");
        myDB.open();
    }

    protected void onStop() {
        super.onStop();
        if (myDB != null) {
            myDB.close();
        }
    }

    public void register(View view) {
        music.startMusic(3);
        String username = editText1.getText().toString().trim();
        String password1 = editText2.getText().toString();
        String password2 = editText3.getText().toString();
        String md5Psw = MD5Utils.md5(password1);

        if (checkUsername(username)) {
            Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
        } else if (!password1.equals(password2)) {
            Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
        } else if (password1.length() < 6) {
            Toast.makeText(this, "密码不能小于6个字符", Toast.LENGTH_SHORT).show();
        } else {
            if (checkRegister(username, md5Psw, 0, "顽强青铜")) {
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                editor = saved.edit();
                editor.putString("name", username)
                        .putString("pass", md5Psw)
                        .putString("lastName", username)
                        .putBoolean("isLogin", true)
                        .commit();
                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);

            }
        }

    }

    //插入数据
    private boolean checkRegister(String username, String md5Psw, int score, String title) {
        User user = new User(username, md5Psw, score, title);
        myDB.insert(user);
        return true;

    }

    //检验用户名是否存在
    private boolean checkUsername(String username) {
        Cursor cursor = myDB.queryName(username);
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        return false;
    }
}
